#!/usr/bin/env python

# -------------------------------
# projects/python/collatz/main.py
# Copyright (C) 2009
# -------------------------------

# To run the program
#     main.py < Collatz.in > Collatz.out

# To document the program
#     pydoc -w main

import sys
import math

# -------
# globals
# -------

num_cases = 0 # input: don't change after reading
num_candidates = 0 # input: don't change after reading
all_candidates = []  # list for all candidates
ballots = [] # a list for all ballots
winners = [] # a list for all winners
num_votes = [] # a list to track number of votes for each candidate
first_case = True # a boolean to check a blank line after number of cases
blankLine = False
fblankLine = True
reass_index = 0
lists = [] # a list to stoe a list of ballots for each candidate
losers = [] # a list to strore losers' ballots

# -----------
# InputReader
# -----------

class InputReader (object) :
    def read (self) :
        return raw_input()

# ------------
# OutputWriter
# ------------

class OutputWriter (object) :
    def write (self, *a) :
        for w in a :
            print w,
        print

# -------
# my_read
# -------

def num_cases (r) :
    """
    reads an int into num_cases
    return true if that succeeds, false otherwise
    """
    global num_cases   
    num_cases = 0

    s = r.read()
    l = s.split()
    if len(l) > 1 :
    	return False
    num_cases = int(l[0])
    return True

def my_read(r):
    """
    This method reads a blank line after number of cases,
    number of candidates, names of candidates and all ballots.
    returns true if everything succeeds, false otherwise
    """
    global num_candidates
    global num_cases
    global ballots   
    global all_candidates
    global first_case
    global fblankLine

    num_candidates = 0  # at the beginning of reading input num_candidates has $
    ballots = []        # at the beginning of reading input ballots have to emp$
    all_candidates = []
    first_case = True
   # blank line
    if fblankLine :
	fblankLine = False
	try:
		s= r.read()
    	except EOFError:
		return False
    	if len(s)>0:
		return False
   #get number of candidates
    try :
        s = r.read()
    except EOFError :
        return False
    l = s.split()
    num_candidates = int(s)
    if num_candidates > 20 :
        return False
    # reading input for candidate names
    temp = 1
    while temp <= num_candidates:
        try :
            s = r.read()
        except EOFError:
            return False
	if len(s) > 80 :
	    return False
        all_candidates.append(s)
        temp+=1
    # get ballots
    temp_list = []
    bool = True
    while bool:
        try :
            s = r.read()
        except EOFError :
            return False
        l = s.split()
        if len(l) == 0 :
            break
	temp_list = []
	index = 0
        temp = 0
        while temp < num_candidates :
            temp_list.append(int(l[index]))
            temp +=1
	    index+=1
        ballots.append(temp_list)
        if len(ballots) > 1000 :
	    return False
        temp_list = []
    return True

def findLosers(list):
    """
    This method returns a list of ballots of all losers
    """
    global num_votes
    global losers
    temp_list = []
    leader = list[0]
    listlen = len(list)
    leader_index = 0
    #find leaders length
    max_length = 0
    for m in list:
    	if max_length < len(m):
	    max_length = len(m)
    index = 0
    while listlen > index :
        if max_length > len(list[index]) :# and len(list[index])!=0 :
            for v in list[index] :
                temp_list.append(v)
            num_votes[index] -= num_votes[index]
        elif max_length == len(list[index]) :
            pass
        else:
            for v in leader :
                temp_list.append(v)
            leader = list[index]
	    num_votes[leader_index] -= num_votes[leader_index]
	    leader_index = index
        index+=1
    return temp_list

#------------
# winner_exist
#------------

def winner_exist():
	"""
	This method checks if there a winner or not.
	If there is a winner it returns true, otherwise false
	"""
	global num_votes
	global ballots
	global all_candidates
	global winners
	num_ballots = len(ballots)
	answer = False
	winner_index = 0
	if num_ballots == 0:
		winners = all_candidates
	if len(winners)>0:
		answer = True
		return answer
	for v in num_votes :
        	if (v*1.0/num_ballots) > 0.5 :
			answer = True
            		winners.append(all_candidates[winner_index])
        	winner_index+=1
	return answer

#--------
# isTie()
#-------
def isTie(list):
	"""
	This method check if there is a tie,
	if so, returns true, otherwise false
	"""
	global num_votes
	global winners
	global all_candidates
	index = 0
	isTie = True
	temp = 0
        #add all tied candidates to winners list
        if len(list) == 0:
                for v in num_votes:
                        if v != temp:
                                winners.append(all_candidates[index])
                        index+=1
		return isTie
	# find non-zero number in num_votes list
	for v in num_votes:
		if v != 0:
			temp = v
			break
	#check if other than temp is exist in num_votes
	for v in num_votes:
		if v !=temp and v!=0:
			isTie = False
			return isTie
	return isTie

#--------
# isDeleteLosersBallots
#--------

def isDeleteLosersBallots(v):
	"""
	This method deletes all ballots of loosersin the list of list ballots.
	"""
	global lists
	global reass_index
	temp = lists[v[reass_index-1]-1]
	index = 0
	while index < len(temp):
        	if v == temp[index]:
			temp[index]=[]
			break
		index+=1
        temp = lists[v[reass_index-1]-1]
        index = 0
	l = []
	while index < len(temp):
		if len(temp[index])!=0:
			l.append(temp[index])
		index+=1	
	lists[v[reass_index-1]-1] = l

#--------
# reassign
#-------

def reassign(list):
	"""
	This method reassigns ballots of losers
	"""
	global reass_index
	global lists
	temp_index = 0
	#increment votes in num_votes
	reass_index+=1
        for v in list : # list is a list of losers's ballots
        	temp_index = v[reass_index]
                num_votes[temp_index-1]+=1
		lists[temp_index-1].append(v) # append to a new cadidate
		isDeleteLosersBallots(v)

# -------
# my_eval
# -------

def my_eval () :
    """
    computes the one or more winners of Australian Voting
    
    """
    global num_candidates
    global ballots
    global winners
    global all_candidates
    global num_votes
    global lists
    global losers

    num_ballots = len(ballots)
    num_votes = []
    lists = []
    winners = []
    for v in all_candidates : # for loop to create a list of votes that each candidate received
        num_votes.append(0)
    # put empty lists to "lists"
    temp = num_candidates
    while temp>0 :
        lists.append([])
        temp-=1
    index = 0
    for n in ballots :
        temp_index = n[index]
        lists[temp_index-1].append(n)
        num_votes[temp_index-1]+=1
    if not(winner_exist()):
    	losers = findLosers(lists)
	isTie(losers)
	while not(winner_exist()):
		reassign(losers)
		losers = findLosers(lists)
		isTie(losers)

# --------
# my_print
# --------

def my_print (w) :
    """
    writes the name of one or more winner candidates
    """
    global winners
    while len(winners)> 0 :
        w.write(winners.pop(0))

# ----
# main
# ----

def main () :
	"""
	runs the program
     	"""
	global num_cases
	global reass_index
	global num_candidates
	if num_cases(InputReader()) :
		temp = 0
		while temp < num_cases:
			temp+=1
			reass_index = 0
			num_candidates = 0
			my_read(InputReader())
        		my_eval()
        		my_print(OutputWriter())
        		if temp < num_cases:
	        		print # blank line
        		if temp == num_cases:
        			break

if __name__ == "__main__" :
    main()
